package loveWaves.compatibility.tests;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.hp.hpl.jena.ontology.*;

import com.hp.hpl.jena.rdf.model.*;

import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.RDF;

public class TestPouet {

	public static OntModel model;
	public static String urlOnt ="file:///Users/julien/Documents/workspace/loveWaves/ontology/loveWavesRDF.rdfs";
	public static String  urlInstances ="file:///Users/julien/Documents/workspace/loveWaves/ontology/loveWavesRDF.rdf";
	public static String urlProfMale="file:///Users/julien/Documents/workspace/loveWaves/testProfiles/Shaukat.rdf";
	public static String urlProfFemale="file:///Users/julien/Documents/workspace/loveWaves/testProfiles/Anna.rdf";
	
	public static void main(String[] args){
		model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF);
		model.read(urlOnt);
		model.read(urlInstances);
		
		OntModel modelProf1 = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF);
		OntModel modelProf2 = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF);
		modelProf1.read(urlProfMale);
		modelProf2.read(urlProfFemale);
		model.addSubModel(modelProf1);
		model.addSubModel(modelProf2);
		StmtIterator iter = model.listStatements();
		while(iter.hasNext()){
			System.out.println(iter.nextStatement());
		}
		System.out.println("------------------------------------------------------");
		Selector sel = new SimpleSelector(model.getResource("http://purl.org/NET/S3MW3BTeam/lw/profiles#anna"),null,(Object)null);
		StmtIterator iter2 = model.listStatements(sel);
		while(iter2.hasNext()){
			System.out.println(iter2.nextStatement());
		}
		System.out.println("------------------------------------------------------");
		OntProperty datingProperty = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#datingProperty");
		//ExtendedIterator iter3 = model.listOntProperties();
		ExtendedIterator iter3 = datingProperty.listSubProperties(false);
		while(iter3.hasNext()){
			OntProperty currentProp = (OntProperty)iter3.next();
			if(currentProp.getLabel("fr")!=null){
				System.out.println(currentProp.getLabel("fr"));
			}
		}
		System.out.println("------------------------------------------------------");
		OntClass language = model.getOntClass("http://purl.org/NET/S3MW3BTeam/lw#Language");
		ExtendedIterator iter4 = language.listInstances();
		while(iter4.hasNext()){
			OntResource resource = (OntResource)iter4.next();
			System.out.println(resource.getURI());
			System.out.println(resource.getLabel("en-GB"));
		}
		System.out.println("------------------------------------------------------");
		OntClass dater = model.getOntClass("http://purl.org/NET/S3MW3BTeam/lw#Dater");
		ExtendedIterator iter5 = dater.listInstances();
		Set<OntResource> daters = new HashSet<OntResource>();
		/*while(iter5.hasNext()){
			OntResource daterInst = (OntResource)iter5.next();
			daters.add(daterInst);
			System.out.println(daterInst.getURI());
		}*/
		OntResource requester = (OntResource)iter5.next();
		OntResource requested = (OntResource)iter5.next();
		OntProperty ageMin = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#lookForAgeMin");
		OntProperty ageMax = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#lookForAgeMax");
		OntProperty age = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#age");
		OntProperty music = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#music");
		OntProperty movie = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#movies");
		OntProperty cuisine = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#favouriteCuisine");
		
		int ageMinRequester = Integer.parseInt((requester.getPropertyValue(ageMin)).toString());
		int ageMaxRequester = Integer.parseInt((requester.getPropertyValue(ageMax)).toString());
		
		int ageRequested = Integer.parseInt((requested.getPropertyValue(age)).toString());
		
		System.out.println(ageMinRequester+" "+ageMaxRequester);
		System.out.println(ageRequested);
		
		Set<Resource> musicRequester = new HashSet<Resource>();
		Set<Resource> musicRequested = new HashSet<Resource>();
		Set<Resource> movieRequester = new HashSet<Resource>();
		Set<Resource> movieRequested = new HashSet<Resource>();
		Set<Resource> cuisineRequester = new HashSet<Resource>();
		Set<Resource> cuisineRequested = new HashSet<Resource>();
		
		NodeIterator requesterMSNodeIter = requester.listPropertyValues(music);
		while(requesterMSNodeIter.hasNext()){
			Resource res = (Resource)requesterMSNodeIter.next();
			musicRequester.add(res);
			System.out.println(res.getURI());
		}
		
		NodeIterator requestedMSNodeIter = requested.listPropertyValues(music);
		while(requestedMSNodeIter.hasNext()){
			Resource res = (Resource)requestedMSNodeIter.next();
			musicRequested.add(res);
			System.out.println(res.getURI());
		}
		
		NodeIterator requesterMVNodeIter = requester.listPropertyValues(movie);
		while(requesterMVNodeIter.hasNext()){
			Resource res = (Resource)requesterMVNodeIter.next();
			movieRequester.add(res);
			System.out.println(res.getURI());
		}
		
		Property movieTitle = model.getOntProperty("http://purl.org/NET/S3MW3BTeam/lw#movieTitle");
		NodeIterator requestedMVNodeIter = requested.listPropertyValues(movie);
		while(requestedMVNodeIter.hasNext()){
			Resource res = (Resource)requestedMVNodeIter.next();
			StmtIterator iterInter = res.listProperties(movieTitle);
			while(iterInter.hasNext()){
				System.out.println(iterInter.nextStatement());
			}
			movieRequested.add(res);
			System.out.println(res.getURI());
		}
		
		NodeIterator requesterCNodeIter = requester.listPropertyValues(cuisine);
		while(requesterCNodeIter.hasNext()){
			Resource res = (Resource)requesterCNodeIter.next();
			cuisineRequester.add(res);
			System.out.println(res.getURI());
		}
		
		NodeIterator requestedCNodeIter = requested.listPropertyValues(cuisine);
		while(requestedCNodeIter.hasNext()){
			Resource res = (Resource)requestedCNodeIter.next();
			cuisineRequested.add(res);
			System.out.println(res.getURI());
		}

		
		System.out.println("------------------------------------");
		System.out.println("MATCHES");
		System.out.println("------------------------------------");
		System.out.println("Age");
		System.out.println((ageRequested >= ageMinRequester) && (ageRequested <= ageMaxRequester));
		System.out.println("music");
		musicRequested.retainAll(musicRequester);
		System.out.println(musicRequested);
		System.out.println("movies");
		movieRequested.retainAll(movieRequester);
		System.out.println(movieRequested);
		System.out.println("cuisine");
		cuisineRequested.retainAll(cuisineRequester);
		System.out.println(cuisineRequested);
		NodeIterator nodeIter = requester.listPropertyValues(RDF.type);
		while(nodeIter.hasNext()){
			OntClass male = model.getOntClass("http://purl.org/NET/S3MW3BTeam/lw#Male");
			OntClass female = model.getOntClass("http://purl.org/NET/S3MW3BTeam/lw#Female");
			System.out.println(male);
			Resource res = (Resource)nodeIter.next();
			System.out.println(res.getURI());
			System.out.println((res).equals((Resource)male));
		}
		
		model.removeSubModel(modelProf1);
		model.removeSubModel(modelProf2);
	}
	
}
